/* * Copyright the original author or authors. *  * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *	  http://www.mozilla.org/MPL/MPL-1.1.html *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.googlecode.milib.tools {	import flash.geom.Point;	/**
	 * @author Marek Brun
	 */
	public class Vector {
		
		public var x:Number;		
		public var y:Number;		
		
		public function Vector($x:Number=0, $y:Number=0) { 
			x=$x;
			y=$y;
		}
		
		public function getLength():Number { 
			var ret:Number=Math.sqrt(x*x+y*y); 
			if(isNaN(ret)){
				return 0;
			}else{
				return ret;
			}
		}
		
		public function setLength(newLength:Number):void {
			var length:Number=getLength();
		  	if(length){ scale(newLength/length); }else{ x=newLength; }
		}
		
		public function getAngle():Number { 
			return Math.atan2(y, x); 
		}
		
		public function setAngle(newAngle:Number):void { 
			var length:Number=getLength(); 
			x=length*Math.cos(newAngle); 
			y=length*Math.sin(newAngle);
		}
		
		public function scale(newScale:Number):void {
			x*=newScale;
			y*=newScale;
		}
		
		public function movePoint(point:Point, moveScale01:Number):void {
			point.x+=x*moveScale01;
			point.y+=y*moveScale01;
		}
		
		public function plus(vector:Vector):void {
			x+=vector.x;
			y+=vector.y;
		}
		
		public function minus(vector:Vector):void { 
			x-= vector.x ;
			y-= vector.y;
		}				public function normalize():void {			var normal:Number=Math.sqrt((x*x)+(y*y));			x/=normal;			y/=normal;		}
		
	}
}
